package com.woniuxy.stage3demo.controller.chenyuxuanController;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.chenyuxuanBean.DeptBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.chenyxuanService.IDeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 部门查询控制器
 */
@RequestMapping("/dept")
@RestController
public class DeptController {
    @Resource
    private IDeptService deptServiceimpl;

    /**
     * 修改部门信息
     * @param deptBean
     * @return
     */
    @PutMapping(value = "/update",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(@RequestBody DeptBean deptBean){
        System.out.println("-------------修改部门信息");
        System.out.println(deptBean+"-------------修改部门信息");
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        int row=deptServiceimpl.updateDept(deptBean);
        if(row == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg deleteDept(@PathVariable("id") Long id){
        System.out.println("这是删除部门controller"+id);
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null) {
            deptServiceimpl.deleteDept(id.intValue());
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }

    /**
     * 添加部门
     *
     * @param deptBean
     */
    @PostMapping(value = "/add", produces = {"application/json;charset=utf-8"})
    public ResultMsg addDept(@RequestBody DeptBean deptBean) {
        System.out.println(deptBean+"这是添加部门信息");
        deptBean.setDelFlag("0");
        deptBean.setStatus("1");
        Date date = new Date();
        deptBean.setCreateTime(date);
        ResultMsg resultMsg = new ResultMsg(200, "新增成功", null);
        System.out.println(deptBean);
        deptServiceimpl.addDept(deptBean);
        return resultMsg;
    }

    /**
     * 分页查询部门详情
     *
     * @return
     */
    @RequestMapping(value = "/page", produces = {"application/json;charset=utf-8"})
    public TableResult<DeptBean> findAllDept(PageRequest pageRequest, DeptBean deptBean) {
       /* System.out.println(pageRequest);*/
        System.out.println(deptBean);
        PageInfo<DeptBean> pageInfo = deptServiceimpl.findAllDept(pageRequest, deptBean);
        System.out.println("分页查询信息:"+pageInfo);
        return new TableResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 修改部门状态
     *
     * @param id
     * @param status
     * @return
     */
    @PutMapping(value = "/status/{id}/{status}", produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id, @PathVariable("status") String status) {
        ResultMsg res = new ResultMsg(200, "操作成功", null);
        System.out.println(id);
        System.out.println(status);
        if (id != null && status != null) {
            DeptBean deptBean = new DeptBean();
            deptBean.setId(id);
            deptBean.setStatus(status);
            deptServiceimpl.updateDept(deptBean);
            return res;
        } else {
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
}
